package com.example.module_service.adapter

import android.view.View
import androidx.databinding.DataBindingUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.module_service.R
import com.example.module_service.databinding.ItemParkBinding
import com.example.module_service.park.ParkBeanItem
import com.example.lib_common.R as CommonR

class ParkPageAdapter : BaseQuickAdapter<ParkBeanItem, ParkPageAdapter.ParkViewHolder>(R.layout.item_park) {

    private var mOnItemClickListener: OnItemClickListener? = null
    private var mOnItemLongClickListener: OnItemLongClickListener? = null

    class ParkViewHolder(view: View) : BaseViewHolder(view) {
        // 原有的 binding 相关逻辑
        val binding: ItemParkBinding = DataBindingUtil.bind(view)!!
    }

    override fun convert(holder: ParkViewHolder, item: ParkBeanItem) {
        holder.binding.apply {
            parkNum.text = item.spaceNumber
            parkArea.text = "所属区域：${item.areaName}"
            parkType.text = "车位类型：${item.spaceType}"
            parkUser.text = "使用人：${item.userName}"
            parkCarNo.text = "车牌号：${item.licensePlate}"
            parkStatus.text = item.spaceStatus

            when (item.spaceStatus) {
                "空闲" -> {
                    parkStatus.setTextColor(holder.itemView.context.getColor(R.color.green))
                    parkStatus.setBackgroundResource(CommonR.drawable.shape_status)
                }
                "已占用" -> {
                    parkStatus.setTextColor(holder.itemView.context.getColor(R.color.blue))
                    parkStatus.setBackgroundResource(CommonR.drawable.shape_zhanyong)
                }
                else -> {
                    parkStatus.setTextColor(holder.itemView.context.getColor(R.color.orange))
                    parkStatus.setBackgroundResource(CommonR.drawable.shape_weihu)
                }
            }
        }

        holder.itemView.setOnClickListener {
            mOnItemClickListener?.onItemClick(holder.adapterPosition, item)
        }

        holder.itemView.setOnLongClickListener {
            mOnItemLongClickListener?.onItemLongClick(item)
            true
        }
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.mOnItemClickListener = listener
    }

    fun setOnItemLongClickListener(listener: OnItemLongClickListener) {
        this.mOnItemLongClickListener = listener
    }
}

interface OnItemClickListener {
    fun onItemClick(position: Int, item: ParkBeanItem)
}

interface OnItemLongClickListener {
    fun onItemLongClick(item: ParkBeanItem)
}